package com.service.impl;

import com.entity.Employee;
import com.exception.UsernameOrPasswordWrongException;
import com.mapper.EmployeeMapper;
import com.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(String ename, String password) {
        Employee employee = employeeMapper.selectByEname(ename);
        if(employee == null){
            throw new UsernameOrPasswordWrongException("用户名或者密码错误");
        }
        if(!(DigestUtils.md5DigestAsHex(password.getBytes()).equals(employee.getPassword()))){
            throw new UsernameOrPasswordWrongException("用户名或者密码错误");
        }
        return employee;
    }

    @Override
    public List<Employee> find() {
        List<Employee> all = employeeMapper.findAll();
        List<Employee> list = new ArrayList<>();
        for (Employee employee : all) {
            if(employee.getRid().equals(1)){
                list.add(employee);
            }
        }
        return list;
    }

}
